<?php

class EventsController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Events');
		$this->view->setVar('content', 'events/index');
		$this->view->setVar('title', 'Events');
    }
    public function detailsAction()
    {
        $this->view->setMainView('index');
        $this->view->setVar('page_name', 'Events');
        $this->view->setVar('content', 'events/details');
        $this->view->setVar('title', 'Events');
    }

    public function createAction()
    {
        $data = array(
                "creator_id"=>$this->session->get('user_id'),
                "title"=>$this->request->getPost('title'),
                "when"=>$this->request->getPost('when'),
                "venue"=>$this->request->getPost('venue'),
                "content"=>$this->request->getPost('content'),
                "group_id"=>$this->request->getPost('group_id'),
                "long"=>$this->request->getPost('longt'),
                "lat"=>$this->request->getPost('lat'),
                "city"=>$this->request->getPost('city')
            );
        $group_id = $this->request->getPost('group_id'); 
        
        $info = UserAccountTb::findFirstById($data['creator_id']);
        
        $receivers = GroupMembers::find(array(
                "conditions"=>"group_id = ?0",
                "bind"=>array(0=>$group_id),
                "columns"=>"user_id"
            ));

        $ids = array();
        foreach ($receivers as $value) {
            $ids[] = $value->user_id;
        }
        $result = array();

        $event = new Events();
        if(!$event->save($data)){
            $result['status'] = false;
            echo json_encode($result);
            $this->view->disable();
            exit();
        }
        $data['event_id'] = $event->id;
        $data2 = array(
            'receivers'=> implode(",", $ids),
            'feed_title'=>nl2br($data['title']),
            'params'=> json_encode($data),
            'feed_type'=> "events",
            'fullname' => $info->fname.' '.$info->lname,
            'prof_pic' => IMAGE_URL_SMALL.$info->profile_pic,
            'sender_id' => $this->session->get('user_id'),
            'sender_fullname' => UserAccountTb::getFullname($data['creator_id']),
            "creator_id"=>$this->session->get('user_id'),
            "title"=>$this->request->getPost('title'),
            "when"=>$this->request->getPost('when'),
            "venue"=>$this->request->getPost('venue'),
            "content"=>$this->request->getPost('content'),
            "event_id"=>$data['event_id']
        );

        $feeds = new UserFeedsTb();
        $feeds->save($data2);
        $data2['date_created'] = $feeds->date_created;
        $data2['feed_id'] = $feeds->id;
        $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
        $pusher->trigger('my-channel', 'new_event', $data2);
        
        $result['status'] = true;
        echo json_encode($result);
        $this->view->disable();
    }

    public function insertStatusAction()
    {
    	$event_id = $this->request->getPost('event_id');
    	$status = $this->request->getPost('status');
    	$user_id = $this->session->get('user_id');
    	$result = array();

    	$event_status = UserEventStatus::findFirst(array(
    			'conditions'=>"event_id = ?0 AND user_id = ?1",
    			'bind'=>array(0=>$event_id,1=>$user_id)
    		));

    	if(!$event_status){
    		$event_status = new UserEventStatus();
    		$event_status->user_id = $user_id;
    		$event_status->event_id = $event_id;
    	}
    		$event_status->status = $status;

    	if(!$event_status->save()){  
    		$result['status'] = false;
    		echo json_encode($result);
    		$this->view->disable();
    		exit();
    	}

        $result['total_coming'] = UserEventStatus::find(array(
                "conditions"=>"event_id = ?0 and status='coming'",
                "bind"=>array(0=>$event_id)
            ))->count();
		$result['status'] = true;
		echo json_encode($result);
		$this->view->disable();

    }
}

